/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ControlPanel.java
 *
 * Created on Aug 21, 2009, 11:20:03 AM
 */

package org.plotnikov.pricemaker.panels;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.jdesktop.application.Action;
import org.plotnikov.pricemaker.patterns.observer.PriceListInterface;
import org.plotnikov.pricemaker.patterns.observer.DrawObject;

/**
 *
 * @author alexey
 */
public class ControlPanel extends javax.swing.JPanel{

    private List<PriceListInterface> observable = new Vector<PriceListInterface>();
    private DrawObject refreshObj = null;
    private int movePixels = 2;

    /** Creates new form ControlPanel */
    public ControlPanel() {
        initComponents();
    }

    public boolean addObserver(PriceListInterface observer){
        return observable.add(observer);
    }

    public boolean removeObserver(PriceListInterface observer){
        return observable.remove(observer);
    }

    public void setPanelName(String name){
        setBorder(javax.swing.BorderFactory.createTitledBorder(name));
    }

    public void setRefreshObj(DrawObject refreshObj) {
        jSpinner1.setValue(refreshObj.getFontSize());
        this.refreshObj = refreshObj;
    }

    public void setMovePixels(int movePixels) {
        this.movePixels = movePixels;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bUp = new javax.swing.JButton();
        bRight = new javax.swing.JButton();
        bLeft = new javax.swing.JButton();
        bDown = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jSpinner1 = new javax.swing.JSpinner();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.plotnikov.pricemaker.PriceMakerApp.class).getContext().getResourceMap(ControlPanel.class);
        setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("Form.border.title"))); // NOI18N
        setName("Form"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(org.plotnikov.pricemaker.PriceMakerApp.class).getContext().getActionMap(ControlPanel.class, this);
        bUp.setAction(actionMap.get("Up")); // NOI18N
        bUp.setText(resourceMap.getString("bUp.text")); // NOI18N
        bUp.setName("bUp"); // NOI18N

        bRight.setAction(actionMap.get("right")); // NOI18N
        bRight.setText(resourceMap.getString("bRight.text")); // NOI18N
        bRight.setName("bRight"); // NOI18N

        bLeft.setAction(actionMap.get("left")); // NOI18N
        bLeft.setText(resourceMap.getString("bLeft.text")); // NOI18N
        bLeft.setName("bLeft"); // NOI18N

        bDown.setAction(actionMap.get("down")); // NOI18N
        bDown.setText(resourceMap.getString("bDown.text")); // NOI18N
        bDown.setName("bDown"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jSpinner1.setModel(new javax.swing.SpinnerNumberModel(12, 1, 72, 1));
        jSpinner1.setName("jSpinner1"); // NOI18N
        jSpinner1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner1StateChanged(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(bLeft)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 51, Short.MAX_VALUE)
                .add(bRight))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(83, Short.MAX_VALUE)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSpinner1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 80, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .add(layout.createSequentialGroup()
                .add(68, 68, 68)
                .add(bUp)
                .addContainerGap(72, Short.MAX_VALUE))
            .add(layout.createSequentialGroup()
                .add(71, 71, 71)
                .add(bDown)
                .addContainerGap(75, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jSpinner1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(bUp)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(bLeft)
                    .add(bRight))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(bDown))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner1StateChanged
        if (refreshObj!=null){
            refreshObj.setFontSize(((Integer)jSpinner1.getValue()).intValue());
            fireObservers();
        }
    }//GEN-LAST:event_jSpinner1StateChanged

    private void fireObservers(){
        for (Iterator<PriceListInterface> it = observable.iterator(); it.hasNext();) {
            PriceListInterface priceListObservableI = it.next();
            priceListObservableI.refreshLayer(refreshObj);
        }
    }

    @Action
    public void Up() {
        if (refreshObj!=null){
            refreshObj.up(movePixels);
            fireObservers();
        }
    }

    @Action
    public void left() {
        if (refreshObj!=null){
            refreshObj.left(movePixels);
            fireObservers();
        }
    }

    @Action
    public void right() {
        if (refreshObj!=null){
            refreshObj.right(movePixels);
            fireObservers();
        }
    }

    @Action
    public void down() {
        if (refreshObj!=null){
            refreshObj.down(movePixels);
            fireObservers();
        }
    }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bDown;
    private javax.swing.JButton bLeft;
    private javax.swing.JButton bRight;
    private javax.swing.JButton bUp;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSpinner jSpinner1;
    // End of variables declaration//GEN-END:variables

}
